Overview

The Dundas BI REST services use objects that belong to the Dundas.BI.WebApi.Models Namespace. When using .NET, these objects can be serialized and deserialized using JSON as the communication language.

How to serialize a Dundas BI model object to JSON

The following example demonstrates how to create a LogOnOptions object, serialize it as a JSON string, and then send it to the Dundas BI REST call for logging on. This sample uses the Newtonsoft.Json library that is in the Dundas BI SDK folder.

using Dundas.BI.WebApi.Models;
using Newtonsoft.Json;
using System.Net.Http;
   ...
using (HttpClient httpClient = new HttpClient())
{
    string logonUri = "http://localhost:8004/Api/LogOn/";
    LogOnOptions logOnOptions = new LogOnOptions()
    {
		AccountName = "admin",
		Password = "1234",
		DeleteOtherSessions = true,
		IsWindowsLogOn = false
    };
    // Serialize the logon options object.
    string requestBodyAsString = JsonConvert.SerializeObject(logOnOptions);
    // Prepare the request content
    StringContent content = new StringContent(
	    requestBodyAsString,
	    Encoding.UTF8,
	    "application/json"
	);
    string returnJsonString = string.Empty;
    using (var response = httpClient.PostAsync(logonUri, content).Result)
	{
    	returnJsonString = response.Content.ReadAsStringAsync().Result;
	}
}
    

How to deserialize a string to a Dundas BI model object

The following example extends the previous example to receive a JSON string and serialize it to a LogOnResultData object.

using Dundas.BI.WebApi.Models;
using Newtonsoft.Json;
using System.Net.Http;
   ...
using (HttpClient httpClient = new HttpClient())
{
    string logonUri = "http://localhost:8004/Api/LogOn/";
    LogOnOptions logOnOptions = new LogOnOptions()
    {
		AccountName = "admin",
		Password = "1234",
		DeleteOtherSessions = true,
		IsWindowsLogOn = false
    };
    // Serialize the logon options object.
    string requestBodyAsString = JsonConvert.SerializeObject(logOnOptions);
    // Prepare the request content
    StringContent content = new StringContent(
		requestBodyAsString,
		Encoding.UTF8,
		"application/json"
	);
    string returnJsonString = string.Empty;
    using (var response = httpClient.PostAsync(logonUri, content).Result)
	{
    	returnJsonString = response.Content.ReadAsStringAsync().Result;
	}
    LogOnResultData logOnResultData = JsonConvert.DeserializeObject<LogOnResultData>(returnJsonString);
	
	if (logOnResultData.LogOnFailureReason.Equals(Dundas.BI.AccountServices.LogOnFailureReason.None))
	{
		// Logon was successful.
	}
}